package com.java.utils;

import java.awt.Image;
import java.io.File;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

/**
 * 通用工具类
 * @author kevin
 *
 */
public class Tools {

	/**
	 * 在size范围内 获取不重复num个数的下标
	 * 
	 * @param size
	 * @param num
	 * @return
	 */
	public static List<Integer> getRandowIndexs(List<Integer> indexs, int num) {
		Set<Integer> set = new HashSet<Integer>();
		Random rd = new Random();
		for (int n = 0; n < num; n++) {
			int rdIndex = rd.nextInt(indexs.size());
			int index = indexs.get(rdIndex);
			set.add(index);
			indexs.remove(rdIndex);
		}
		List<Integer> result = new ArrayList<Integer>();
		result.addAll(set);
		Collections.sort(result);
		return result;
	}

	/**
	 * double 类型四舍五入取整
	 * 
	 * @param d
	 * @return
	 */
	public static int toIntRoundHalfUp(double d) {
		return new BigDecimal(d).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
	}

	/**
	 * 获取图片的属性，来校验图片是否可以正常显示
	 * 
	 * @param imageFile
	 * @return
	 */
	public static boolean isImage(File imageFile) {
		if (!imageFile.exists()) {
			return false;
		}
		Image img = null;
		try {
			img = ImageIO.read(imageFile);
			if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
				return false;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			img = null;
		}
	}

	public static boolean isPicture(String fileName) {
		if (fileName == null || fileName.isEmpty()) {
			return false;
		} else {
			if (fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".JPG")
					|| fileName.endsWith(".PNG"))
				return true;
			else
				return false;
		}
	}

	/**
	 * 正则验证手机号
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone) {
		String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
		if (phone.length() != 11) {
			return false;
		} else {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(phone);
			boolean isMatch = m.matches();
			return isMatch;
		}
	}

	/**
	 * 返回指定长度的随机数字符串
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomNum(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(random.nextInt(10));
		}
		return sb.toString();
	}

	public static String getToken() {
		return UUID.randomUUID().toString();
	}

	/**
	 * MD5加密
	 * 
	 * @return
	 */
	public static String MD5(String message) {
		String md5str = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 加入固定的盐
			message = message + "salt";
			byte[] input = message.getBytes();
			byte[] buff = md.digest(input);
			md5str = bytesToHex(buff);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return md5str;
	}

	/**
	 * 二进制转十六进制
	 * 
	 * @param bytes
	 * @return
	 */
	private static String bytesToHex(byte[] bytes) {
		StringBuffer md5str = new StringBuffer();
		int digital;
		for (int i = 0; i < bytes.length; i++) {
			digital = bytes[i];
			if (digital < 0) {
				digital += 256;
			}
			if (digital < 16) {
				md5str.append("0");
			}
			md5str.append(Integer.toHexString(digital));
		}
		return md5str.toString().toUpperCase();
	}
}
